//
//  CZVistorTableController.swift
//  weibo27
//
//  Created by mac on 16/4/16.
//  Copyright © 2016年 it.cast. All rights reserved.
//

import UIKit

class CZVistorTableController: UITableViewController {
    
    //定义一个属性标记用户是否登陆过
    private var userLogin = CZUserAccountViewModel.shareModel.userLogin
    
    var visitorView: CZVisitorView?
    
    override func loadView() {
        //如果用户已经登陆了，就直接显示，没有登录拦截，自己定义视图
        userLogin ? super.loadView() : setupVisitorView()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: UIBarButtonItemStyle.Plain, target: self, action: "CZVisitorViewDidRegister")
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: UIBarButtonItemStyle.Plain, target: self, action: "CZVisitorViewDidLogin")
        
    }

}

extension CZVistorTableController{
    
    func setupVisitorView(){
        
        visitorView = CZVisitorView()
        
        //用创建的view替换原来的跟视图、
        view = visitorView
        
        //直接使用addtarget添加监听事件
        visitorView?.registerButton.addTarget(self, action: "CZVisitorViewDidRegister", forControlEvents: UIControlEvents.TouchUpInside)
        
        visitorView?.loginButton.addTarget(self, action: "CZVisitorViewDidLogin", forControlEvents: UIControlEvents.TouchUpInside)
        
    }
}

/// 实现代理的方法
extension CZVistorTableController{
    
    /// 注册
    func CZVisitorViewDidRegister(){
        
        print("注册")
    }
    
    /// 登录  跳到登录授权的页面
    func CZVisitorViewDidLogin(){
        
        let vc = CZOAuthView()
        
        let nav = UINavigationController(rootViewController: vc)
        
        navigationController?.presentViewController(nav, animated: true, completion: nil)
        
        
    }
}














